package gptm

import (
	"chatgpt/model/base"
	"github.com/slclub/go-tips"
)

type Tokens struct {
	TokensID int    `gorm:"primaryKey;column:tokens_id;autoIncrement"`
	Date     string `gorm:"size:20;comment:日期"`
	Model    string `gorm:"size:128;comment:chat 模型"`
	Total    int    `gorm:"type:int(10);comment:chat gpt 花销tokens"`
}

func (self *Tokens) TableName() string {
	return "gpt_tokens"
}

func (self *Tokens) ConvFromMap(m map[string]any) {
	if val, ok := m["TokensID"]; ok && self.TokensID == 0 {
		self.TokensID = tips.Int(val)
	}
	if val, ok := m["Date"]; ok {
		self.Date = tips.String(val)
	}
	if val, ok := m["Model"]; ok {
		self.Model = tips.String(val)
	}
	if val, ok := m["Total"]; ok {
		self.Total = tips.Int(val)
	}
}

func (self *Tokens) ConvToMap() map[string]any {
	return map[string]any{
		"TokensID": self.TokensID,
		"Date":     self.Date,
		"Model":    self.Model,
		"Total":    self.Total,
	}
}

// tokens_user
type TokensUser struct {
	base.ModelID
	base.UUIDR
	TokensID       int `gorm:"type:int(10);default:0;column:tokens_id;comment:某一个模型的ID"`
	TokensGpt      int `gorm:"type:int(10);default:0;comment:gpt的tokens数量"`
	TokensInternal int `gorm:"type:int(10);default:0;comment:内部的tokens数量"`
}

func (self *TokensUser) TableName() string {
	return "gpt_tokens_user"
}

func (self *TokensUser) ConvFromMap(m map[string]any) {
	if val, ok := m["TokensID"]; ok {
		self.TokensID = tips.Int(val)
	}
	if val, ok := m["TokensGpt"]; ok {
		self.TokensGpt = tips.Int(val)
	}
	if val, ok := m["TokensInternal"]; ok {
		self.TokensInternal = tips.Int(val)
	}
	self.ModelID.ConvFromMap(m)
	self.UUIDR.ConvFromMap(m)
}

func (self *TokensUser) ConvToMap() map[string]any {
	return map[string]any{
		"ID":             self.ID,
		"UUID":           self.UUID,
		"TokensID":       self.TokensID,
		"TokensGpt":      self.TokensGpt,
		"TokensInternal": self.TokensInternal,
	}
}
